﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;

namespace DARE
{
    //public class CWorldCreator
    //{

    //#region fields

    //    private CRender3D m_render;
    //    private XmlDocument m_xmlDoc = new XmlDocument();
    //    private string m_xmlWorld;

    //#endregion

    //#region properties

    //    public string XmlWorld
    //    {
    //        get { return m_xmlWorld; }
    //    }

    //    public CRender3D Render3D
    //    {
    //        get { return m_render; }
    //    }

    //    //public CHeightMapInfo HeightMapInfo
    //    //{
    //    //    get { return m_render.HeightMapInfo; }
    //    //}

    //#endregion

    //#region ctor

    //    public CWorldCreator()
    //    {
    //        Initialize(CDare.Instance.SceneMgr.ActiveScene.ActiveRender3D);
    //    }

    //    public CWorldCreator(CRender3D render)
    //    {
    //        Initialize(render);
    //    }

    //    private void Initialize(CRender3D render)
    //    {
    //        m_render = render;
    //    }

    //#endregion

    //#region world

    //    public void CreateWorld(string worldPath)
    //    {
    //        DestroyWorld();
    //        //m_render.HeightMapInfo = null;
    //        TextReader tr = new StreamReader("Content/XML/" + worldPath + ".xml");
    //        m_xmlWorld = tr.ReadToEnd();
    //        tr.Close();
    //        ParseWorld();
    //    }

    //    public void SaveWorld(string worldPath)
    //    {
    //        throw new Exception("SaveWorld not implemented yet");
    //    }

    //    public void DestroyWorld()
    //    {
    //        CDare.Instance.AudioMgr.MusicMgr.UnloadContent();
    //        CDare.Instance.AudioMgr.SoundMgr.Unload();
    //        CDare.Instance.Config.Audio.ResetPath();
    //        m_render.ResetPhysics();
    //        m_render.CameraMgr.UnregisterCameras();
    //        //m_render.DestroyNodes();
    //        m_render.RemoveNodes();
    //    }

    //#endregion

    //#region parsing

    //    private void ParseWorld()
    //    {
    //        m_xmlDoc.LoadXml(m_xmlWorld);
    //        foreach (XmlNode root in m_xmlDoc.ChildNodes)
    //            switch (root.Name)
    //            {
    //                case "world":
    //                    foreach (XmlNode child in root.SelectNodes("directory"))
    //                        ParseDirectory(child);
    //                    foreach (XmlNode node in root.ChildNodes)
    //                        ParseElement(node);
    //                    break;

    //                default: break;
    //            }
    //    }

    //    private void ParseDirectory(XmlNode dir)
    //    {
    //        foreach (XmlNode child in dir.ChildNodes)
    //            switch (child.Name)
    //            {
    //                case "root": CDare.Instance.Config.General.RootDirectory = GetDirectory(child); break;
    //                case "music": CDare.Instance.Config.General.MusicDirectory = GetDirectory(child); break;
    //                case "sound": CDare.Instance.Config.General.SoundDirectory = GetDirectory(child); break;
    //                case "model": CDare.Instance.Config.General.ModelDirectory = GetDirectory(child); break;
    //                case "particle": CDare.Instance.Config.General.ParticleDirectory = GetDirectory(child); break;
    //                default: break;
    //            }
    //    }

    //    private void ParseElement(XmlNode element)
    //    {
    //        if ((element = ParseComment(element)) == null)
    //            return;
    //        switch (element.Name)
    //        {
    //            case "node": ParseNode(element); break;
    //            case "camera": ParseCamera(element); break;
    //            case "audio": ParseAudio(element); break;
    //            default: Console.WriteLine("Unknown element: " + element.Name); break;
    //        }
    //    }

    //#endregion

    //#region node

    //    private void ParseNode(XmlNode node, CNode dareNode = null)
    //    {
    //        string nodeName = null;
    //        bool nodeOnHeightMap = false;
    //        Vector3 nodePosition = Vector3.Zero;
    //        Vector3 nodeOrientation = Vector3.Zero;
    //        Vector3 nodeScale = Vector3.One;

    //        if (node.Attributes["name"] != null)
    //            nodeName = node.Attributes["name"].Value;
    //        if (node.Attributes["onheightmap"] != null)
    //            nodeOnHeightMap = bool.Parse(node.Attributes["onheightmap"].Value);
    //        foreach (XmlNode child in node.SelectNodes("position|orientation|scale"))
    //            switch (child.Name)
    //            {
    //                case "position":
    //                    nodePosition.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    nodePosition.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    nodePosition.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "orientation":
    //                    nodeOrientation.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    nodeOrientation.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    nodeOrientation.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "scale":
    //                    nodeScale.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    nodeScale.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    nodeScale.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                default: break;
    //            }
    //        // node creation
    //        if (dareNode == null)
    //            dareNode = m_render.OriginNode;
    //        //if ((nodeOnHeightMap == true) && (m_render.HeightMapInfo != null))
    //        //    nodePosition.Y += m_render.HeightMapInfo.GetHeight(nodePosition);
    //        dareNode = dareNode.CreateChild(nodeName);
    //        dareNode.SetPosition(nodePosition);
    //        dareNode.SetOrientation(
    //            Quaternion.CreateFromYawPitchRoll(
    //            MathHelper.ToRadians(nodeOrientation.Y), 
    //            MathHelper.ToRadians(nodeOrientation.X), 
    //            MathHelper.ToRadians(nodeOrientation.Z)));
    //        dareNode.SetScale(nodeScale);
    //        foreach (XmlNode child in node.SelectNodes("model|physic|heightmap|basicmodel|audiosound|emitters|sprite3d"))
    //            switch (child.Name)
    //            {
    //                case "model": ParseModel(child, dareNode); break;
    //                case "physic": ParsePhysic(child, dareNode); break;
    //                case "heightmap": ParseHeightMap(child, dareNode); break;
    //                case "basicmodel": ParseBasicModel(child, dareNode); break;
    //                case "audiosound": ParseAudioSound(child, dareNode); break;
    //                case "emitters": ParseEmitters(child, dareNode); break;
    //                case "sprite3d": ParseSprite3D(child, dareNode); break;
    //                default: break;
    //            }
    //        foreach (XmlNode child in node.SelectNodes("node"))
    //            ParseNode(child, dareNode);
    //    }

    //    private void ParseModel(XmlNode model, CNode dareNode)
    //    {
    //        CModel modelModel = null;
    //        string modelName = null;
    //        string modelPath = null;
    //        Vector3 modelOrientation = Vector3.Zero;
    //        Vector3 modelScale = Vector3.One;
    //        Vector3 modelOffset = Vector3.Zero;

    //        if ((model.Attributes["name"] != null) && (model.Attributes["path"] != null))
    //        {
    //            modelName = model.Attributes["name"].Value;
    //            if (CDare.Instance.Config.General.ModelDirectory != null)
    //                modelPath = CDare.Instance.Config.General.ModelDirectory + "/" + model.Attributes["path"].Value;
    //            else
    //                modelPath = model.Attributes["path"].Value;
    //        }
    //        foreach (XmlNode child in model.SelectNodes("orientation|scale|offset"))
    //            switch (child.Name)
    //            {
    //                case "orientation":
    //                    modelOrientation.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    modelOrientation.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    modelOrientation.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "scale":
    //                    modelScale.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    modelScale.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    modelScale.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "offset":
    //                    modelOffset.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    modelOffset.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    modelOffset.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                default: break;
    //            }
    //        if ((modelName != null) && (modelPath != null))
    //        {
    //            modelModel = new CModel(modelName, modelPath);
    //            modelModel.Orientation = Matrix.CreateFromYawPitchRoll(
    //                MathHelper.ToRadians(modelOrientation.Y), 
    //                MathHelper.ToRadians(modelOrientation.X), 
    //                MathHelper.ToRadians(modelOrientation.Z));
    //            modelModel.Scale = modelScale;
    //            modelModel.Offset = modelOffset;
    //            dareNode.Entity.AttachModel(modelModel);
    //        }
    //    }

    //    private void ParsePhysic(XmlNode physic, CNode dareNode)
    //    {
    //        bool physicImmovable = false;
    //        bool physicAllowFreezing = true;
    //        APhysicObject.ECollisionType collisionType = APhysicObject.ECollisionType.UNDEFINED;
    //        APhysicObject physicObject = null;
    //        CModel model = null;

    //        if (physic.Attributes["immovable"] != null)
    //            physicImmovable = bool.Parse(physic.Attributes["immovable"].Value);
    //        if (physic.Attributes["allowfreezing"] != null)
    //            physicAllowFreezing = bool.Parse(physic.Attributes["allowfreezing"].Value);
    //        if (physic.Attributes["collisiontype"] != null)
    //            switch (physic.Attributes["collisiontype"].Value)
    //            {
    //                case "undefined": collisionType = APhysicObject.ECollisionType.UNDEFINED; break;
    //                case "none": collisionType = APhysicObject.ECollisionType.NONE; break;
    //                case "ground": collisionType = APhysicObject.ECollisionType.GROUND; break;
    //                case "fade": collisionType = APhysicObject.ECollisionType.FADE; break;
    //                case "clickable": collisionType = APhysicObject.ECollisionType.CLICKABLE; break;
    //                case "trigger": collisionType = APhysicObject.ECollisionType.TRIGGER; break;
    //                default: break;
    //            }
    //        foreach (XmlNode child in physic.SelectNodes("plane|box|cylinder|sphere|capsule|physictriangle|physicapprox"))
    //            switch (child.Name)
    //            {
    //                case "plane":
    //                    physicObject = dareNode.Entity.CreatePhysicPlane(
    //                        float.Parse(child.Attributes["size"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Physic;
    //                    break;

    //                case "box":
    //                    physicObject = dareNode.Entity.CreatePhysicBox(
    //                        float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                        float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                        float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Physic;
    //                    break;

    //                case "cylinder":
    //                    physicObject = dareNode.Entity.CreatePhysicCylinder(
    //                        float.Parse(child.Attributes["length"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                        float.Parse(child.Attributes["diameter"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Physic;
    //                    break;

    //                case "sphere":
    //                    physicObject = dareNode.Entity.CreatePhysicSphere(
    //                        float.Parse(child.Attributes["diameter"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Physic;
    //                    break;

    //                case "capsule":
    //                    physicObject = dareNode.Entity.CreatePhysicCapsule(
    //                        float.Parse(child.Attributes["length"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                        float.Parse(child.Attributes["diameter"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Physic;
    //                    break;

    //                case "physictriangle":
    //                    bool includeNonColl = false;
    //                    model = dareNode.Entity.Model as CModel;

    //                    if (model != null)
    //                    {
    //                        if (child.Attributes["includenoncoll"] != null)
    //                            includeNonColl = bool.Parse(child.Attributes["includenoncoll"].Value);
    //                        physicObject = new CPhysicMesh(model, includeNonColl);
    //                        dareNode.Entity.AttachPhysic(physicObject);
    //                    }
    //                    else
    //                        Console.WriteLine("A triangle mesh collider cannot be created on node " + dareNode.Name 
    //                            + " because it has no CModel available.");
    //                    break;

    //                case "physicapprox":
    //                    model = dareNode.Entity.Model as CModel;

    //                    if (model != null)
    //                    {
    //                        physicObject = new CPhysicMeshApprox(model);
    //                        dareNode.Entity.AttachPhysic(physicObject);
    //                    }
    //                    else
    //                        Console.WriteLine("An approximative bounding box cannot be created on node " + dareNode.Name 
    //                            + " becaise is has no CModel available.");
    //                    break;

    //                default: break;
    //            }
    //        if (physicObject != null)
    //        {
    //            physicObject.Body.Immovable = physicImmovable;
    //            physicObject.Body.AllowFreezing = physicAllowFreezing;
    //            physicObject.CollisionType = collisionType;
    //        }
    //    }

    //    private void ParseHeightMap(XmlNode heightmap, CNode dareNode)
    //    {
    //        Texture2D heightMapTex = null;
    //        bool physicHeightMap = false;
    //        APhysicObject.ECollisionType collisionType = APhysicObject.ECollisionType.GROUND;

    //        if (heightmap.Attributes["path"] != null)
    //            heightMapTex = CResourceMgr.Load<Texture2D>(heightmap.Attributes["path"].Value);
    //        if (heightmap.Attributes["physic"] != null)
    //            physicHeightMap = bool.Parse(heightmap.Attributes["physic"].Value);
    //        if (heightmap.Attributes["collisiontype"] != null)
    //            switch (heightmap.Attributes["collisiontype"].Value)
    //            {
    //                case "undefined": collisionType = APhysicObject.ECollisionType.UNDEFINED; break;
    //                case "none": collisionType = APhysicObject.ECollisionType.NONE; break;
    //                case "ground": collisionType = APhysicObject.ECollisionType.GROUND; break;
    //                case "fade": collisionType = APhysicObject.ECollisionType.FADE; break;
    //                case "clickable": collisionType = APhysicObject.ECollisionType.CLICKABLE; break;
    //                case "trigger": collisionType = APhysicObject.ECollisionType.TRIGGER; break;
    //                default: break;
    //            }
    //        foreach (XmlNode child in heightmap.SelectNodes("size"))
    //            switch (child.Name)
    //            {
    //                case "size":
    //                    //m_render.HeightMapInfo = new CHeightMapInfo(heightMapTex, 
    //                    //    float.Parse(child.Attributes["bump"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                    //    float.Parse(child.Attributes["scale"].Value, CDare.Instance.Config.General.NumberFormatInfo));
    //                    break;

    //                default: break;
    //            }
    //        if (physicHeightMap == true)
    //        {
    //            //dareNode.Entity.CreatePhysicHeightmap(m_render.HeightMapInfo);
    //            //dareNode.Entity.Physic.CollisionType = collisionType;
    //        }
    //    }

    //    private void ParseBasicModel(XmlNode basicmodel, CNode dareNode)
    //    {
    //        CBasicModel basicModel = null;
    //        CullMode basicCullMode = CullMode.CullCounterClockwiseFace;
    //        FillMode basicFillMode = FillMode.WireFrame;
    //        Vector3 basicOrientation = Vector3.Zero;

    //        if (basicmodel.Attributes["cullmode"] != null)
    //            switch (basicmodel.Attributes["cullmode"].Value)
    //            {
    //                case "clockwiseface": basicCullMode = CullMode.CullClockwiseFace; break;
    //                case "counterclockwisefase": basicCullMode = CullMode.CullCounterClockwiseFace; break;
    //                case "none": basicCullMode = CullMode.None; break;
    //                default: break;
    //            }
    //        if (basicmodel.Attributes["fillmode"] != null)
    //            switch (basicmodel.Attributes["fillmode"].Value)
    //            {
    //                case "solid": basicFillMode = FillMode.Solid; break;
    //                case "wireframe": basicFillMode = FillMode.WireFrame; break;
    //                default: break;
    //            }
    //        foreach (XmlNode child in basicmodel.SelectNodes("orientation|plane|box|cylinder|sphere|capsule|heightmap"))
    //            switch (child.Name)
    //            {
    //                case "orientation":
    //                    basicOrientation.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    basicOrientation.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    basicOrientation.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "plane":
    //                    basicModel = dareNode.Entity.CreateBasicPlane(
    //                        float.Parse(child.Attributes["size"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Model 
    //                        as CBasicModel;
    //                    break;

    //                case "box":
    //                    basicModel = dareNode.Entity.CreateBasicBox(
    //                        float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                        float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                        float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Model 
    //                        as CBasicModel;
    //                    break;

    //                case "cylinder":
    //                    basicModel = dareNode.Entity.CreateBasicCylinder(
    //                        float.Parse(child.Attributes["length"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                        float.Parse(child.Attributes["diameter"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Model 
    //                        as CBasicModel;
    //                    break;

    //                case "sphere":
    //                    basicModel = dareNode.Entity.CreateBasicSphere(
    //                        float.Parse(child.Attributes["diameter"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Model
    //                        as CBasicModel;
    //                    break;

    //                case "capsule":
    //                    basicModel = dareNode.Entity.CreateBasicCapsule(
    //                        float.Parse(child.Attributes["length"].Value, CDare.Instance.Config.General.NumberFormatInfo),
    //                        float.Parse(child.Attributes["diameter"].Value, CDare.Instance.Config.General.NumberFormatInfo)).Model
    //                        as CBasicModel;
    //                    break;

    //                case "heightmap":
    //                    //if (m_render.HeightMapInfo != null)
    //                    //    basicModel = dareNode.Entity.CreateBasicHeightmap(
    //                    //        m_render.HeightMapInfo).Model
    //                    //        as CBasicModel;
    //                    break;

    //                default: break;
    //            }
    //        if (basicModel != null)
    //        {
    //            basicModel.FillMode = basicFillMode;
    //            basicModel.CullMode = basicCullMode;
    //            basicModel.Orientation = Matrix.CreateFromYawPitchRoll(
    //                basicOrientation.X, basicOrientation.Y, basicOrientation.Z);
    //        }
    //    }

    //    private void ParseAudioSound(XmlNode audiosound, CNode dareNode)
    //    {
    //        foreach (XmlNode child in audiosound.ChildNodes)
    //            switch (child.Name)
    //            {
    //                case "sound":
    //                    if ((child.Attributes["name"] != null) && (child.Attributes["fullname"] != null))
    //                    {
    //                        string soundName = child.Attributes["name"].Value;
    //                        string soundFullName = child.Attributes["fullname"].Value;

    //                        dareNode.Entity.AttachSound(soundName, soundFullName);
    //                    }
    //                    break;

    //                default: break;
    //            }
    //    }

    //    private void ParseEmitters(XmlNode emitters, CNode dareNode)
    //    {
    //        foreach (XmlNode child in emitters.ChildNodes)
    //            switch (child.Name)
    //            {
    //                case "emitter": ParseEmitter(child, dareNode); break;

    //                default: break;
    //            }
    //    }

    //    private void ParseEmitter(XmlNode emitter, CNode dareNode)
    //    {
    //        AEmitter emitterEmitter = null;
    //        string emitterPath = null;
    //        string emitterName = null;
    //        bool emitterStarted = false;

    //        if (emitter.Attributes["path"] == null)
    //            return;
    //        emitterPath = CDare.Instance.Config.General.AbsoluteParticlePath + emitter.Attributes["path"].Value;
    //        if (emitter.Attributes["name"] != null)
    //            emitterName = emitter.Attributes["name"].Value;
    //        if (emitter.Attributes["started"] != null)
    //            emitterStarted = bool.Parse(emitter.Attributes["started"].Value);

    //        TextReader tr = new StreamReader(emitterPath);
    //        string xml = tr.ReadToEnd();

    //        tr.Close();
    //        if (xml == null)
    //            return;
    //       /* emitterEmitter = CDare.Instance.EmitterMgr.LoadEmitter(xml);
    //        if (emitterEmitter != null)
    //        {
    //            emitterEmitter.Name = emitterName;
    //            dareNode.Entity.AttachEmitter(emitterEmitter);
    //            emitterEmitter.IsStarted = emitterStarted;
    //        }*/
    //    }

    //    private void ParseSprite3D(XmlNode sprite3d, CNode dareNode)
    //    {
    //        foreach (XmlNode child in sprite3d.ChildNodes)
    //            switch (child.Name)
    //            {
    //                case "linetext3d": ParseLineText3D(child, dareNode); break;

    //                default: break;
    //            }
    //    }

    //    private void ParseLineText3D(XmlNode linetext3d, CNode dareNode)
    //    {
    //        string lineName = null;
    //        string lineText = null;
    //        Vector3 lineOffset = Vector3.Zero;

    //        if (linetext3d.Attributes["name"] != null)
    //            lineName = linetext3d.Attributes["name"].Value;
    //        if (linetext3d.Attributes["text"] != null)
    //            lineText = linetext3d.Attributes["text"].Value;
    //        foreach (XmlNode child in linetext3d.SelectNodes("offset"))
    //            switch (child.Name)
    //            {
    //                case "offset":
    //                    lineOffset.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    lineOffset.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    lineOffset.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                default: break;
    //            }
    //        if (lineName != null)
    //        {
    //            CLineText3D lineText3d = new CLineText3D(lineName);

    //            lineText3d.Text = lineText;
    //            lineText3d.Offset = lineOffset;
    //            dareNode.Entity.AttachSprite3D(lineText3d);
    //        }
    //    }

    //#endregion

    //#region camera

    //    private void ParseCamera(XmlNode camera)
    //    {
    //        foreach (XmlNode child in camera.SelectNodes("firstperson|iso|fixed|rts|thirdperson"))
    //            switch (child.Name)
    //            {
    //                case "firstperson": ParseFirstPerson(child); break;
    //                case "iso": ParseIso(child); break;
    //                case "fixed": ParseFixed(child); break;
    //                case "rts": ParseRts(child); break;
    //                case "thirdperson": ParseThirdPerson(child); break;
    //                default: break;
    //            }
    //    }

    //    private void ParseFirstPerson(XmlNode firstperson)
    //    {
    //        string cameraName = null;
    //        bool cameraActive = false;
    //        Vector3 cameraMove = Vector3.Zero;
    //        Vector3 cameraOrientate = Vector3.Zero;
    //        CFirstPersonCamera cameraFirst = null;

    //        if (firstperson.Attributes["name"] != null)
    //            cameraName = firstperson.Attributes["name"].Value;
    //        if (firstperson.Attributes["active"] != null)
    //            cameraActive = bool.Parse(firstperson.Attributes["active"].Value);            
    //        foreach (XmlNode child in firstperson.SelectNodes("move|orientate"))
    //            switch (child.Name)
    //            {
    //                case "move":
    //                    cameraMove.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    cameraMove.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    cameraMove.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "orientate":
    //                    cameraOrientate.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    cameraOrientate.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    cameraOrientate.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                default: break;
    //            }
    //        //cameraFirst = new CFirstPersonCamera(cameraName, m_render);
    //        cameraFirst = new CFirstPersonCamera(cameraName);
    //        //cameraFirst = new CFirstPersonCamera(cameraName);
    //        //cameraFirst.Translate(cameraMove);
    //        cameraFirst.Translate(cameraMove);
    //        //cameraFirst.Rotate(Quaternion.CreateFromYawPitchRoll(cameraOrientate.Y, cameraOrientate.X, cameraOrientate.Z));
    //        cameraFirst.Rotate(cameraOrientate);
    //        m_render.CameraMgr.RegisterCamera(cameraFirst);
    //        if (cameraActive == true)
    //            m_render.CameraMgr.SetActiveCamera(cameraFirst);
    //    }

    //    private void ParseIso(XmlNode iso)
    //    {
    //        string cameraName = null;
    //        bool cameraActive = false;
    //        Vector3 cameraFollowPosition = Vector3.One;
    //        bool cameraInterpolateMove = false;
    //        bool cameraInterpolateZoom = false;
    //        float cameraInterpolateMoveSpeed = 0.007f;
    //        float cameraInterpolateZoomSpeed = 0.005f;
    //        float cameraZoomMin = 0.2f;
    //        float cameraZoomMax = 1.0f;
    //        float cameraZoom = 1.0f;
    //        CIsoCamera cameraIso = null;

    //        if (iso.Attributes["name"] != null)
    //            cameraName = iso.Attributes["name"].Value;
    //        if (iso.Attributes["active"] != null)
    //            cameraActive = bool.Parse(iso.Attributes["active"].Value);
    //        foreach (XmlNode child in iso.SelectNodes("followposition|interpolatemove|interpolatezoom|zoom"))
    //            switch (child.Name)
    //            {
    //                case "followposition": 
    //                    cameraFollowPosition.X = float.Parse(child.Attributes["x"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    cameraFollowPosition.Y = float.Parse(child.Attributes["y"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    cameraFollowPosition.Z = float.Parse(child.Attributes["z"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "interpolatemove": 
    //                    cameraInterpolateMove = true;
    //                    if (child.Attributes["speed"] != null)
    //                        cameraInterpolateMoveSpeed = float.Parse(child.Attributes["speed"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "interpolatezoom": 
    //                    cameraInterpolateZoom = true;
    //                    if (child.Attributes["speed"] != null)
    //                        cameraInterpolateZoomSpeed = float.Parse(child.Attributes["speed"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "zoom": 
    //                    if (child.Attributes["value"] != null)
    //                    {
    //                        cameraZoom = float.Parse(child.Attributes["value"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                        Console.WriteLine("zoom value not working yet, need the node to follow");
    //                    }
    //                    if (child.Attributes["min"] != null)
    //                        cameraZoomMin = float.Parse(child.Attributes["min"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    if (child.Attributes["max"] != null)
    //                        cameraZoomMax = float.Parse(child.Attributes["max"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                default: break;
    //            }
    //        //cameraIso = new CIsoCamera(cameraName, m_render);
    //        cameraIso = new CIsoCamera(cameraName);
    //        cameraIso.FollowPosition = cameraFollowPosition;
    //        cameraIso.Smooth = cameraInterpolateMove;
    //        cameraIso.SmoothAmount = cameraInterpolateMoveSpeed;
    //        //cameraIso.InterpolateZoom = cameraInterpolateZoom;
    //        //cameraIso.InterpolateZoomSpeed = cameraInterpolateZoomSpeed;
    //        cameraIso.ZoomMin = cameraZoomMin;
    //        cameraIso.ZoomMax = cameraZoomMax;
    //        cameraIso.Zoom = cameraZoom;
    //        m_render.CameraMgr.RegisterCamera(cameraIso);
    //        if (cameraActive == true)
    //            m_render.CameraMgr.SetActiveCamera(cameraIso);
    //    }

    //    private void ParseFixed(XmlNode fixe)
    //    {
    //        Console.WriteLine("Not implemented yet");
    //    }

    //    private void ParseRts(XmlNode rts)
    //    {
    //        Console.WriteLine("Not implemented yet");
    //    }

    //    private void ParseThirdPerson(XmlNode thirdperson)
    //    {
    //        Console.WriteLine("Not implemented yet");
    //    }

    //#endregion

    //#region audio

    //    private void ParseAudio(XmlNode audio)
    //    {
    //        foreach (XmlNode child in audio.ChildNodes)
    //            switch (child.Name)
    //            {
    //                case "audiomusic": ParseAudioMusic(child); break;
    //                case "soundcreation": ParseSoundCreation(child); break;
    //                default: break;
    //            }
    //    }

    //    private void ParseSoundCreation(XmlNode soundcreation)
    //    {
    //        string path = "";

    //        if (CDare.Instance.Config.General.SoundDirectory != null)
    //            path += CDare.Instance.Config.General.SoundDirectory + "/";
    //        foreach (XmlNode child in soundcreation.ChildNodes)
    //            switch (child.Name)
    //            {
    //                case "audioengine":
    //                    if (child.Attributes["path"] != null)
    //                        CDare.Instance.Config.Audio.AudioEnginePath = path + child.Attributes["path"].Value;
    //                    break;

    //                case "soundbank":
    //                    if (child.Attributes["path"] != null)
    //                        CDare.Instance.Config.Audio.SoundBankPath = path + child.Attributes["path"].Value;
    //                    break;

    //                case "wavebank":
    //                    if (child.Attributes["path"] != null)
    //                        CDare.Instance.Config.Audio.WaveBankPath = path + child.Attributes["path"].Value;
    //                    break;

    //                default: break;
    //            }
    //        //CDare.Instance.CreateAudio();
    //    }

    //    private void ParseAudioMusic(XmlNode audiomusic)
    //    {
    //        foreach (XmlNode child in audiomusic.ChildNodes)
    //            switch (child.Name)
    //            {
    //                case "option": ParseAudioOption(child); break;
    //                case "music": ParseMusic(child); break;
    //                default: break;
    //            }
    //    }

    //    private void ParseAudioOption(XmlNode option)
    //    {
    //        CMusicMgr musicMgr = CDare.Instance.AudioMgr.MusicMgr;
    //        if (musicMgr == null)
    //            return;

    //        float musicVolume = 0.5f;
    //        bool musicMute = false;
    //        bool musicFade = true;
    //        float musicFadeDuration = 1.0f;
    //        CMusicMgr.EPlayOption musicPlayOption = CMusicMgr.EPlayOption.ALL;
    //        CMusicMgr.ERepeatOption musicRepeatOption = CMusicMgr.ERepeatOption.ALL;

    //        foreach (XmlNode child in option.ChildNodes)
    //            switch (child.Name)
    //            {
    //                case "volume": 
    //                    if (child.Attributes["value"] != null)
    //                        musicVolume = float.Parse(child.Attributes["value"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "mute":
    //                    if (child.Attributes["value"] != null)
    //                        musicMute = bool.Parse(child.Attributes["value"].Value);
    //                    break;

    //                case "fade": 
    //                    if (child.Attributes["value"] != null)
    //                        musicFade = bool.Parse(child.Attributes["value"].Value);
    //                    if (child.Attributes["duration"] != null)
    //                        musicFadeDuration = float.Parse(child.Attributes["duration"].Value, CDare.Instance.Config.General.NumberFormatInfo);
    //                    break;

    //                case "repeat": 
    //                    switch (child.Attributes["value"].Value)
    //                    {
    //                        case "none": musicRepeatOption = CMusicMgr.ERepeatOption.NONE; break;
    //                        case "all": musicRepeatOption = CMusicMgr.ERepeatOption.ALL; break;
    //                        case "music": musicRepeatOption = CMusicMgr.ERepeatOption.MUSIC; break;
    //                        default: break;
    //                    }
    //                    break;

    //                case "play": 
    //                    switch (child.Attributes["value"].Value)
    //                    {
    //                        case "all": musicPlayOption = CMusicMgr.EPlayOption.ALL; break;
    //                        case "music": musicPlayOption = CMusicMgr.EPlayOption.MUSIC; break;
    //                        case "shuffle": musicPlayOption = CMusicMgr.EPlayOption.SHUFFLE; break;
    //                        default: break;
    //                    }
    //                    break;

    //                default: break;
    //            }
    //        musicMgr.MusicVolume = musicVolume;
    //        musicMgr.MuteMusic = musicMute;
    //        musicMgr.FadeBetweenMusic = musicFade;
    //        musicMgr.FadeDuration = new TimeSpan(0, 0, 0, 0, (int)(musicFadeDuration * 1000.0f));
    //        musicMgr.RepeatOption = musicRepeatOption;
    //        musicMgr.PlayOption = musicPlayOption;
    //    }

    //    private void ParseMusic(XmlNode music)
    //    {
    //        CMusicMgr musicMgr = CDare.Instance.AudioMgr.MusicMgr;
    //        if (musicMgr == null)
    //            return;

    //        if ((music.Attributes["name"] != null) && (music.Attributes["path"] != null))
    //        {
    //            string musicName = music.Attributes["name"].Value;
    //            string musicPath = CDare.Instance.Config.General.AbsoluteMusicPath + music.Attributes["path"].Value;

    //            musicMgr.LoadMusic(musicName, musicPath);
    //            if ((music.Attributes["play"] != null) && (music.Attributes["play"].Value == "true"))
    //                musicMgr.PlayMusic(musicName);
    //        }
    //    }

    //#endregion

    //#region utils

    //    private string GetDirectory(XmlNode directory)
    //    {            
    //        return directory.Attributes["directory"] != null ? directory.Attributes["directory"].Value : null;
    //    }

    //    private XmlNode ParseComment(XmlNode node)
    //    {
    //        while (node.NodeType == XmlNodeType.Comment)
    //        {
    //            if (node.HasChildNodes)
    //                node = node.FirstChild;
    //            else
    //                return null;
    //        }
    //        return node; 
    //    }

    //#endregion

    //}
}
